package com.chuangjiangx.member.manager.web.web.basic.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;

/**
 * @author cj-ch
 * @date 2018/6/1 下午5:09
 */
@Slf4j
@Controller
@RequestMapping(value = "/api/mbr/auth")
public class MbrAuthController {

    @Value("${member.h5.domain}")
    private String h5Domain;

    /**
     * isv公众号授权重定向
     *
     * @param code     授权code
     * @param state    授权参数，可以填写a-zA-Z0-9的参数值，此处存放商户id
     * @param response response
     * @throws Exception
     */
    @RequestMapping(value = "/wx-isv-redirect")
    public void isvRredirect(@RequestParam String code,
                              @RequestParam String state,
                              Long merchantId,
                              HttpServletResponse response) throws Exception {

        StringBuilder str = new StringBuilder(h5Domain);
        str.append("/h5/auth/wx-isv-callback?merchantId=")
                .append(merchantId)
                .append("&code=")
                .append(code)
                .append("&state=")
                .append(state);
        String url = str.toString();
        log.info("公众号h5会员授权重定向地址***************{}" , url);
        response.sendRedirect(url);
    }

    /**
     * 商户公众号授权重定向
     *
     * @param code     授权code
     * @param state    授权参数，可以填写a-zA-Z0-9的参数值，此处存放商户id
     * @param response response
     * @throws Exception
     */
    @RequestMapping(value = "/wx-merchant-redirect")
    public void merchantRredirect(@RequestParam String code,
                              @RequestParam String state,
                              String appid,
                              Long merchantId,
                              HttpServletResponse response) throws Exception {

        StringBuilder str = new StringBuilder(h5Domain);
        str.append("/h5/auth/wx-callback?merchantId=")
                .append(merchantId)
                .append("&code=")
                .append(code)
                .append("&state=")
                .append(state)
                .append("&appid=")
                .append(StringUtils.defaultIfBlank(appid, ""));
        String url = str.toString();
        log.info("公众号h5会员授权重定向地址***************{}" , url);
        response.sendRedirect(url);
    }
}
